unit Class_Prod;

interface
uses
  Classes,SysUtils,Uni,UniEngine;

type
  TProd=class(TUniEngine)
  private
    FUnitLink: string;
    FProdIdex: Integer;
    FProdCode: string;
    FProdName: string;
    FProdMony: Extended;
    FProdOrdr: Integer;
    FProdType: string;
    FProdMemo: string;
    FProdNumb: Integer;
  protected
    procedure SetParameters;override;
    function  GetStrInsert:string;override;
    function  GetStrUpdate:string;override;
    function  GetStrDelete:string;override;
  public
    function  GetStrsIndex:string;override;
  public
    function  GetNextIdex:Integer;overload;
    function  GetNextIdex(AUniConnection:TUniConnection):Integer;overload;
  public
    function  CheckExist(AUniConnection:TUniConnection):Boolean;override;
  public
    destructor  Destroy; override;
    constructor Create;  
  published
    property UnitLink: string read FUnitLink write FUnitLink;
    property ProdIdex: Integer read FProdIdex write FProdIdex;
    property ProdCode: string read FProdCode write FProdCode;
    property ProdName: string read FProdName write FProdName;
    property ProdMony: Extended read FProdMony write FProdMony;
    property ProdOrdr: Integer read FProdOrdr write FProdOrdr;
    property ProdType: string read FProdType write FProdType;
    property ProdMemo: string read FProdMemo write FProdMemo;
    property ProdNumb: Integer read FProdNumb write FProdNumb;    
  public
    class function  ReadDS(AUniQuery:TUniQuery):TUniEngine;override;
    class procedure ReadDS(AUniQuery:TUniQuery;var Result:TUniEngine);override;
    class function  CopyIt(AProd:TProd):TProd;overload;        
    class procedure CopyIt(AProd:TProd;var Result:TProd);overload;       
  end;

implementation

{ TProd }
procedure TProd.SetParameters;
begin
  inherited;
  with FUniSQL.Params do
  begin
    case FOptTyp of
      otAddx:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('PROD_IDEX').Value := PRODIDEX;
        ParamByName('PROD_CODE').Value := PRODCODE;
        ParamByName('PROD_NAME').Value := PRODNAME;
        ParamByName('PROD_MONY').Value := PRODMONY;
        ParamByName('PROD_ORDR').Value := PRODORDR;
        ParamByName('PROD_TYPE').Value := PRODTYPE;
        ParamByName('PROD_MEMO').Value := PRODMEMO; 
      end;
      otEdit:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('PROD_IDEX').Value := PRODIDEX;
        ParamByName('PROD_CODE').Value := PRODCODE;
        ParamByName('PROD_NAME').Value := PRODNAME;
        ParamByName('PROD_MONY').Value := PRODMONY;
        ParamByName('PROD_ORDR').Value := PRODORDR;
        ParamByName('PROD_TYPE').Value := PRODTYPE;
        ParamByName('PROD_MEMO').Value := PRODMEMO; 
      end;  
      otDelt:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('PROD_IDEX').Value := PRODIDEX;
 
      end;  
    end;
  end;
end;

function TProd.CheckExist(AUniConnection: TUniConnection): Boolean;
begin
  Result:=CheckExist('TBL_PROD',['UNIT_LINK',UNITLINK,'PROD_IDEX',PRODIDEX],AUniConnection);
end;

function TProd.GetNextIdex: Integer;
begin

end;

function TProd.GetNextIdex(AUniConnection: TUniConnection): Integer;
begin
  Result:=CheckField('PROD_IDEX','TBL_PROD',['UNIT_LINK',UNITLINK],AUniConnection);
end;

function TProd.GetStrDelete: string;
begin
  Result:='DELETE FROM TBL_PROD WHERE   UNIT_LINK=:UNIT_LINK AND PROD_IDEX=:PROD_IDEX';
end;

function TProd.GetStrInsert: string;
begin
  Result:='INSERT INTO TBL_PROD'
         +'    ( UNIT_LINK, PROD_IDEX, PROD_CODE, PROD_NAME, PROD_MONY'
         +'    , PROD_ORDR, PROD_TYPE, PROD_MEMO)'
         +'    VALUES'
         +'    (:UNIT_LINK,:PROD_IDEX,:PROD_CODE,:PROD_NAME,:PROD_MONY'
         +'    ,:PROD_ORDR,:PROD_TYPE,:PROD_MEMO)';
end;

function TProd.GetStrsIndex: string;
begin
  Result:=Format('%S-%D',[UNITLINK,PRODIDEX]);
end;

function TProd.GetStrUpdate: string;
begin
  Result:='UPDATE  TBL_PROD SET'
         +'    PROD_CODE=:PROD_CODE,'
         +'    PROD_NAME=:PROD_NAME,'
         +'    PROD_MONY=:PROD_MONY,'
         +'    PROD_ORDR=:PROD_ORDR,'
         +'    PROD_TYPE=:PROD_TYPE,'
         +'    PROD_MEMO=:PROD_MEMO'
         +'    WHERE UNIT_LINK=:UNIT_LINK'
         +'    AND   PROD_IDEX=:PROD_IDEX';
end;

constructor TProd.Create;
begin
  UnitLink:='-1';
  ProdNumb:=1;
end;

destructor TProd.Destroy;
begin

  inherited;
end;

class function TProd.ReadDS(AUniQuery: TUniQuery): TUniEngine;
begin
  Result:=TProd.Create;
  with TProd(Result) do
  begin
    UNITLINK:=AUniQuery.FieldByName('UNIT_LINK').AsString;
    PRODIDEX:=AUniQuery.FieldByName('PROD_IDEX').AsInteger;
    PRODCODE:=AUniQuery.FieldByName('PROD_CODE').AsString;
    PRODNAME:=AUniQuery.FieldByName('PROD_NAME').AsString;
    PRODMONY:=AUniQuery.FieldByName('PROD_MONY').AsFloat;
    PRODORDR:=AUniQuery.FieldByName('PROD_ORDR').AsInteger;
    PRODTYPE:=AUniQuery.FieldByName('PROD_TYPE').AsString;
    PRODMEMO:=AUniQuery.FieldByName('PROD_MEMO').AsString;

    if AUniQuery.FindField('PROD_NUMB')<>nil then
    begin
      ProdNumb:=AUniQuery.FieldByName('PROD_NUMB').AsInteger;
    end;

    if AUniQuery.FindField('PROD_MONY_IN_REAL')<>nil then
    begin
      ProdMony:=AUniQuery.FieldByName('PROD_MONY_IN_REAL').AsFloat;
    end;
  end;  
end;

class procedure TProd.ReadDS(AUniQuery: TUniQuery; var Result: TUniEngine);
begin
  if Result=nil then Exit;
  with TProd(Result) do
  begin
    UNITLINK:=AUniQuery.FieldByName('UNIT_LINK').AsString;
    PRODIDEX:=AUniQuery.FieldByName('PROD_IDEX').AsInteger;
    PRODCODE:=AUniQuery.FieldByName('PROD_CODE').AsString;
    PRODNAME:=AUniQuery.FieldByName('PROD_NAME').AsString;
    PRODMONY:=AUniQuery.FieldByName('PROD_MONY').AsFloat;
    PRODORDR:=AUniQuery.FieldByName('PROD_ORDR').AsInteger;
    PRODTYPE:=AUniQuery.FieldByName('PROD_TYPE').AsString;
    PRODMEMO:=AUniQuery.FieldByName('PROD_MEMO').AsString;
  end;  
end;

class function  TProd.CopyIt(AProd: TProd): TProd;
begin
  Result:=TProd.Create;
  TProd.CopyIt(AProd,Result)
end;

class procedure TProd.CopyIt(AProd:TProd;var Result:TProd);
begin
  if Result=nil then Exit;
  Result.UNITLINK:=AProd.UNITLINK;
  Result.PRODIDEX:=AProd.PRODIDEX;
  Result.PRODCODE:=AProd.PRODCODE;
  Result.PRODNAME:=AProd.PRODNAME;
  Result.PRODMONY:=AProd.PRODMONY;
  Result.PRODORDR:=AProd.PRODORDR;
  Result.PRODTYPE:=AProd.PRODTYPE;
  Result.PRODMEMO:=AProd.PRODMEMO;
end;

end.

